package com.oocl.project3;

import java.util.Scanner;




public class Manage<T> {
	
	private Scanner scanner;

	public Person<T> initPerson(Person<T> headperson){
		Person<T> curperson = headperson;
		curperson.setId(1);
		curperson.setName("Clice");
		curperson.setSex("female");
		curperson.setBirthday("1995/11/29");
		curperson.setCall("13212315688");
		curperson.setAddress("hunan");
		Person<T> person2 =new Person<T>(2,"Mike","male","1995/04/23","15172417273","guangdong");
		curperson.setNext(person2); 
		person2.setPer(curperson);
		curperson = curperson.getNext();
		Person<T> person3 =new Person<T>(3,"Bob","male","1995/02/03","13245453232","shenzhen");
		curperson.setNext(person3); 
		person3.setPer(curperson);
		curperson = curperson.getNext();
		Person<T> person4 =new Person<T>(4,"Wyatt","male","1995/09/04","17677775555","wuhan");
		curperson.setNext(person4); 
		person4.setPer(curperson);
		curperson = curperson.getNext();
		Person<T> person5 =new Person<T>(5,"John","male","1995/08/12","13832227675","hunan");
		curperson.setNext(person5); 
		person5.setPer(curperson);
		curperson = curperson.getNext();
		Person<T> person6 =new Person<T>(6,"Jay","male","1993/03/11","17397772323","anhui");
		curperson.setNext(person6); 
		person6.setPer(curperson);
		curperson = curperson.getNext();
		Person<T> person7 =new Person<T>(7,"Alan","female","1995/02/21","15133335555","sichuan");
		curperson.setNext(person7); 
		person7.setPer(curperson);
		curperson = curperson.getNext();
		Person<T> person8 =new Person<T>(8,"Kit","female","1994/11/03","16872324878","hunan");
		curperson.setNext(person8); 
		person8.setPer(curperson);
		curperson = curperson.getNext();
		Person<T> person9 =new Person<T>(9,"Joanne","female","1995/02/22","16732423422","xinjiang");
		curperson.setNext(person9); 
		person9.setPer(curperson);
		curperson = curperson.getNext();
		Person<T> person10 =new Person<T>(10,"Harry","male","1995/07/19","19777772323","hunan");
		curperson.setNext(person10); 
		person10.setPer(curperson);
		
		return headperson;
	}
	
	public Person<T> addPerson(Person<T> headperson){
		System.out.println("Please follow the prompts to enter the information you want to add!!");
		System.out.println("Please input name you want to add!!");
		scanner = new Scanner(System.in);
		String name = scanner.next();
		System.out.println("Please input sex you want to add!!");
		String sex = scanner.next();
		System.out.println("Please input birthday you want to add!!");
		String birthday = scanner.next();
		System.out.println("Please input phone number you want to add!!The format is as follows 'yyyy/mm/dd'!");
		String call = scanner.next();
		System.out.println("Please input address you want to add!!");
		String address = scanner.next();
		Person<T> newperson = new Person<T>();
		Person<T> curperson = headperson;
		while(curperson.getNext()!=null)curperson=curperson.getNext();
		newperson.setId((curperson.getId())+1);
		newperson.setName(name);
		newperson.setSex(sex);
		newperson.setBirthday(birthday);
		newperson.setCall(call);
		newperson.setAddress(address);
		newperson.setPer(curperson);
		curperson.setNext(newperson);
		System.out.println("Add successfully!!");
		return headperson;
	}
	public Person<T> addPerson2(Person<T> headperson,String data){
		Person<T> newperson = new Person<T>();
		String[] arr1 = data.split(",");
		int len = arr1.length;
		try {
			for(int i=0;i<len;i++){
				String[] arr2 = arr1[i].split(":");
				if(arr2[0].equals("name")){
					newperson.setName(arr2[1]);
				}else if (arr2[0].equals("sex")) {
					newperson.setSex(arr2[1]);
				}else if (arr2[0].equals("birthday")) {
					newperson.setBirthday(arr2[1]);
				}else if (arr2[0].equals("call")) {
					newperson.setCall(arr2[1]);
				}else if(arr2[0].equals("address")) {
					newperson.setAddress(arr2[1]);
				}else{
					System.out.println(arr2[0]+"is error field "
							+ "so only the correct field information is added!!!");
				}
			}
			Person<T> curperson = headperson;
			while(curperson.getNext()!=null)curperson=curperson.getNext();
			newperson.setId((curperson.getId())+1);
			newperson.setPer(curperson);
			curperson.setNext(newperson);
			System.out.println("Add successfully!!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("qwqwq");
		}
		return headperson;
	}

	public Person<T> deletePerson(Person<T> headperson){
		Person<T> curperson = headperson;
		System.out.println("Please enter the ID of the message you want to delete!!");
		while(true){
			scanner = new Scanner(System.in);
			int id;
			try {
				id = scanner.nextInt();
				byte flag = 0;
				while(curperson!=null){
					if(curperson.getId()==id){
						if(curperson.getPer()==null){
							headperson = curperson.getNext();
							curperson.setNext(null);
							headperson.setPer(null);
							flag = 1;
							break;
						}else if(curperson.getNext()==null){
							curperson.getPer().setNext(null);
							curperson.setPer(null);
							flag = 1;
							break;
						}else{
							curperson.getPer().setNext(curperson.getNext());
							curperson.getNext().setPer(curperson.getPer());
							curperson.setPer(null);
							curperson.setNext(null);
							flag = 1;
							break;
						}
					}
				curperson = curperson.getNext();
				}
				if(curperson==null&&flag == 0){
					System.out.println("Fail !! Did not find the ID you entered !!!");
				}else {
					System.out.println("Delete successfully");
				}
				break;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println("The characters you entered do not meet the requirements!!!");
				System.out.println("Please input again!!!");
			}
		}
		return headperson;
	}

	public Person<T> updataPerson(Person<T> headperson){
		Person<T> curperson = headperson;
		System.out.println("Please enter the ID of the message you want to updata!!");
		while(true){
			scanner = new Scanner(System.in);
			int id;
			try {
				id = scanner.nextInt();
				byte flag = 0;
				while(curperson!=null){
					if(curperson.getId()==id){
						System.out.println("Please input information you want to add!!The format is as follows "
								+ "name:XXX,sex:xx,birthday:yyyy/mm/dd...");
						String data = scanner.next();
						String[] arr1 = data.split(",");
						int len = arr1.length;
							for(int i=0;i<len;i++){
								String[] arr2 = arr1[i].split(":");
								if(arr2[0].equals("name")){
									curperson.setName(arr2[1]);
								}else if (arr2[0].equals("sex")) {
									curperson.setSex(arr2[1]);
								}else if (arr2[0].equals("birthday")) {
									curperson.setBirthday(arr2[1]);
								}else if (arr2[0].equals("call")) {
									curperson.setCall(arr2[1]);
								}else if(arr2[0].equals("address")) {
									curperson.setAddress(arr2[1]);
								}else{
									System.out.println(arr2[0]+"is error field "
											+ "so only the correct field information is added!!!");
								}
						
							}
					flag = 1;
				}
					curperson = curperson.getNext();
				}
				if(curperson==null&&flag == 0){
					System.out.println("Did not find the ID you entered!!!");
				}else {
					System.out.println("Updata successfully");
				}
				break;
			}catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println("The characters you entered do not meet the requirements!!!");
				System.out.println("Please input again!!!");
			}
		}
		return headperson;
	}

	public Person<T> sortPerson(Person<T> headperson,String condition){
		//Person<T> curperson = headperson;
//		if(headperson==null){
//			System.out.println("该链表不存在！！");
//			return headperson;
//		}
//		if(headperson.getNext()==null)
//			return headperson;
		if(condition.equals("id")){
			for(Person<T> curperson1 = headperson.getNext();curperson1!=null;curperson1=curperson1.getNext()){
				for(Person<T> curperson2 = curperson1;curperson2.getPer()!=null;curperson2=curperson2.getPer()){
					if(curperson2.getId()<curperson2.getPer().getId()){
						int temp =curperson2.getId();
						curperson2.setId(curperson2.getPer().getId());
						curperson2.getPer().setId(temp);
						String temp1 =curperson2.getName();
						curperson2.setName(curperson2.getPer().getName());
						curperson2.getPer().setName(temp1);
						String temp2 =curperson2.getSex();
						curperson2.setSex(curperson2.getPer().getSex());
						curperson2.getPer().setSex(temp2);
						String temp3 =curperson2.getBirthday();
						curperson2.setBirthday(curperson2.getPer().getBirthday());
						curperson2.getPer().setBirthday(temp3);
						String temp4 =curperson2.getCall();
						curperson2.setCall(curperson2.getPer().getCall());
						curperson2.getPer().setCall(temp4);
						String temp5 =curperson2.getAddress();
						curperson2.setAddress(curperson2.getPer().getAddress());
						curperson2.getPer().setAddress(temp5);
//						curperson2.getPer().setNext(curperson2.getNext());
//						curperson2.setPer(curperson2.getPer().getPer());
//						curperson2.getPer().getPer().setNext(curperson2);
//						curperson2.getNext().setPer(curperson2.getPer());
//						curperson2.getPer().setPer(curperson2);
//						curperson2.setNext(curperson2.getPer());
					}
				}
			}
			System.out.println("Sort successfully !!");
		}else{
			for(Person<T> curperson1 = headperson.getNext();curperson1!=null;curperson1=curperson1.getNext()){
				for(Person<T> curperson2 = curperson1;curperson2.getPer()!=null;curperson2=curperson2.getPer()){
					if((curperson2.getName()).compareTo(curperson2.getPer().getName())<0){
						int temp =curperson2.getId();
						curperson2.setId(curperson2.getPer().getId());
						curperson2.getPer().setId(temp);
						String temp1 =curperson2.getName();
						curperson2.setName(curperson2.getPer().getName());
						curperson2.getPer().setName(temp1);
						String temp2 =curperson2.getSex();
						curperson2.setSex(curperson2.getPer().getSex());
						curperson2.getPer().setSex(temp2);
						String temp3 =curperson2.getBirthday();
						curperson2.setBirthday(curperson2.getPer().getBirthday());
						curperson2.getPer().setBirthday(temp3);
						String temp4 =curperson2.getCall();
						curperson2.setCall(curperson2.getPer().getCall());
						curperson2.getPer().setCall(temp4);
						String temp5 =curperson2.getAddress();
						curperson2.setAddress(curperson2.getPer().getAddress());
						curperson2.getPer().setAddress(temp5);
//						curperson2.getPer().setNext(curperson2.getNext());
//						curperson2.setPer(curperson2.getPer().getPer());
//						curperson2.getPer().getPer().setNext(curperson2);
//						curperson2.getNext().setPer(curperson2.getPer());
//						curperson2.getPer().setPer(curperson2);
//						curperson2.setNext(curperson2.getPer());
					}
				}
			}
			System.out.println("Sort successfully !!");
		}
		return headperson;
	}

	public Person<T> lookPerson(Person<T> headperson,String condition){
		Person<T> curperson = headperson;
		if(condition.equals("id")){
			System.out.println("Please input the ID of the information you want to look!!");
			while(true){
				scanner = new Scanner(System.in);
				int id;
				try {
					id = scanner.nextInt();
					byte flag = 0;
					while(curperson!=null){
						if(curperson.getId()==id){

								System.out.println(curperson.getId()+"\t\t"+curperson.getName()+"\t\t"+curperson.getSex()+"\t\t"
								         +curperson.getBirthday()+"\t\t"+curperson.getCall()+"\t\t"+curperson.getAddress());
								flag = 1;
								//break;

						}
					curperson = curperson.getNext();
					}
					if(curperson==null&&flag == 0){
						System.out.println("Fail !! Did not find the ID you entered !!!");
					}else {
						System.out.println("Find successfully !!");
					}
					break;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					System.out.println("The characters you entered do not meet the requirements!!!");
					System.out.println("Please input again!!!");
				}
			}
		}else if (condition.equals("name")) {
			System.out.println("Please input the name of the information you want to look!!");
			while(true){
				scanner = new Scanner(System.in);
				String name;
				try {
					name = scanner.next();
					byte flag = 0;
					while(curperson!=null){
						if(curperson.getName().equals(name)){

								System.out.println(curperson.getId()+"\t\t"+curperson.getName()+"\t\t"+curperson.getSex()+"\t\t"
								         +curperson.getBirthday()+"\t\t"+curperson.getCall()+"\t\t"+curperson.getAddress());
								flag = 1;
								//break;

						}
					curperson = curperson.getNext();
					}
					if(curperson==null&&flag == 0){
						System.out.println("Fail !! Did not find the ID you entered !!!");
					}else {
						System.out.println("Find successfully !!");
					}
					break;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					System.out.println("The characters you entered do not meet the requirements!!!");
					System.out.println("Please input again!!!");
				}
			}
		}else {
			System.out.println("Please input the sex of the information you want to look!!"
					+ "Input male or female");
			while(true){
				scanner = new Scanner(System.in);
				String sex;
				try {
					sex = scanner.next();
					byte flag = 0;
					while(curperson!=null){
						if(curperson.getSex().equals(sex)){

								System.out.println(curperson.getId()+"\t\t"+curperson.getName()+"\t\t"+curperson.getSex()+"\t\t"
								         +curperson.getBirthday()+"\t\t"+curperson.getCall()+"\t\t"+curperson.getAddress());
								flag = 1;
								//break;

						}
					curperson = curperson.getNext();
					}
					if(curperson==null&&flag == 0){
						System.out.println("Fail !! Did not find the sex you entered !!!");
					}else {
						System.out.println("Find successfully !!");
					}
					break;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					System.out.println("The characters you entered do not meet the requirements!!!");
					System.out.println("Please input again!!!");
				}
			}
		}
		return headperson;
	}
	
	public Person<T> printPerson(Person<T> headperson){
		Person<T> curperson = headperson;
		System.out.println("Id\t\tName\t\tSex\t\tBirthday\t\tCall\t\t\tAddress");
		while(curperson!=null){
			System.out.println(curperson.getId()+"\t\t"+curperson.getName()+"\t\t"+curperson.getSex()+"\t\t"
		         +curperson.getBirthday()+"\t\t"+curperson.getCall()+"\t\t"+curperson.getAddress());
			curperson=curperson.getNext();
		}
		return headperson;
	}
}
